package fag.editor;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.EventListenerList;

public class ZoomChooser extends JPanel implements ActionListener {

	private static final long serialVersionUID = 3203920097034108280L;
	
	private final static int DEFAULT_WIDTH = 120;
	private final static int DEFAULT_HEIGHT = 75;
	
	private JButton tbtPlus = new JButton(new ImageIcon(getClass().getResource("/imgs/zoom_plus.png"),"SpriteIcon"));
	private JButton tbtMoins = new JButton(new ImageIcon(getClass().getResource("/imgs/zoom_moins.png"), "HitboxIcon"));
	
	protected final static String ACTION_CHANGED_ZOOM = "Zoom : Changed";
	
	private final EventListenerList listeners = new EventListenerList();
	
	private int currentZoom = 1;
	
	
	public ZoomChooser() {
		
		setBorder(BorderFactory.createTitledBorder("Zoom"));
		
		add(tbtMoins);
		add(tbtPlus);
		
		tbtMoins.setPreferredSize(new Dimension(40,40));
		tbtPlus.setPreferredSize(new Dimension(40,40));
		
		tbtPlus.addActionListener(this);
		tbtMoins.addActionListener(this);
		
		repaint();
	}
	
	public int getCurrentZoom() {
		
		return currentZoom;
	}
	
	@Override
	public Dimension preferredSize() {
		
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	
	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == tbtPlus) {
		
			currentZoom++;
			currentZoomChanged();
		}
		
		if(e.getSource() == tbtMoins) {
			
			if(currentZoom > 1) {
				
				currentZoom--;
				currentZoomChanged();
			}

		}
	}
	
	protected void currentZoomChanged() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_CHANGED_ZOOM));
	}
}
